package com.software.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.software.bean.CustomerInfoBean;
import com.software.bean.ObserverBean;
import com.software.bean.ProductBean;
import com.software.dto.AddObserverDto;
import com.software.dto.ReplyDto;
import com.software.mapper.CustomerInfoMapper;
import com.software.mapper.ObserverMapper;
import com.software.mapper.ProductMapper;
import com.software.service.ObserverService;
import com.software.utils.AssertUtil;
import com.software.vo.ObserverVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wyj
 * @since 2024-04-03
 */
@Service
public class ObserverServiceImpl extends ServiceImpl<ObserverMapper, ObserverBean> implements ObserverService {

    @Autowired
    private ObserverMapper observerMapper;
    @Autowired
    private CustomerInfoMapper customerInfoMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public Integer addObserver(AddObserverDto observerDto) {
        // 参数校验
        AssertUtil.isTrue(StringUtils.isEmpty(observerDto.getObserverContent()), "留言内容不能为空");
        AssertUtil.isTrue(observerDto.getCustomerId() == null, "参数异常");
        AssertUtil.isTrue(observerDto.getProductId() == null, "参数异常");
        //判断参数是否合法
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(observerDto.getCustomerId());
        //判断用户是否存在
        AssertUtil.isTrue(customerInfoBean == null, "用户不存在");
        ProductBean productBean = productMapper.selectById(observerDto.getProductId());
        AssertUtil.isTrue(productBean == null, "商品不存在");
        ObserverBean observerBean = new ObserverBean();
        observerBean.setObserverContent(observerDto.getObserverContent());
        observerBean.setCustomerId(observerDto.getCustomerId());
        observerBean.setProductId(observerDto.getProductId());
        observerBean.setIsDelete(0);
        // 0 代表是第一条
        observerBean.setRootId(0);
        observerBean.setReplyId(0);
        observerBean.setCreateTime(new Date());
        observerBean.setLastId(0);
        int insert = observerMapper.insert(observerBean);
        return insert == 1 ? 1 : 0;
    }

    /**
     * @description:查所有留言列表
     * @author: wyj
     * @date: 2024/4/3 21:04
     * @param:
     * @return:
     */
    @Override
    public List<ObserverVo> getObserverList(Integer productId) {
        AssertUtil.isTrue(productId == null, "商品ID异常");
        ProductBean productBean = productMapper.selectById(productId);
        AssertUtil.isTrue(productBean == null, "商品不存在");
        List<ObserverBean> allObserverListByProductId = observerMapper.getAllObserverListByProductId(productId);
        // 处理顶级评论
        List<ObserverVo> vos = new ArrayList<>();
        for (ObserverBean observerBean : allObserverListByProductId) {
            if (observerBean.getRootId() == 0) {
                ObserverVo observerVo = new ObserverVo();
                observerVo.setObserverBean(observerBean);
                observerVo.setChildren(new ArrayList<>());
                vos.add(observerVo);
            }
        }
        // 处理回复
        for (int i = 0; i < allObserverListByProductId.size(); i++) {
            if (allObserverListByProductId.get(i).getRootId() != 0) {
                for (int j = 0; j < vos.size(); j++) {
                    if (vos.get(j).getObserverBean().getId() == allObserverListByProductId.get(i).getRootId()) {
                        vos.get(j).getChildren().add(allObserverListByProductId.get(i));
                    }
                }
            }
        }
        // 排序
        for (int i = 0; i < vos.size(); i++) {
            vos.get(i).getChildren().sort((o1, o2) -> o1.getCreateTime().compareTo(o2.getCreateTime()));
        }
        return vos;
    }

    /**
     * @description:添加回复功能
     * @author: wyj
     * @date: 2024/4/4 11:44
     * @param:
     * @return:
     */
    @Override
    public Integer addComment(ReplyDto replyDto) {
        System.err.println(replyDto);
        // 参数校验
        AssertUtil.isTrue(replyDto.getObserverId() == null, "参数异常");
        AssertUtil.isTrue(replyDto.getCustomerId() == null, "参数异常");
        AssertUtil.isTrue(replyDto.getProductId() == null, "参数异常");
        AssertUtil.isTrue(StringUtils.isEmpty(replyDto.getReplyContent()), "参数异常");
        //参数合法
        ObserverBean observerBean = observerMapper.selectById(replyDto.getObserverId());
        AssertUtil.isTrue(observerBean == null, "留言不存在");
        CustomerInfoBean customerInfoBean = customerInfoMapper.selectById(replyDto.getCustomerId());
        AssertUtil.isTrue(customerInfoBean == null, "用户不存在");
        ProductBean productBean = productMapper.selectById(replyDto.getProductId());
        AssertUtil.isTrue(productBean == null, "商品不存在");
        ObserverBean replyBean = new ObserverBean();
        replyBean.setObserverContent(replyDto.getReplyContent());
        if (observerBean.getRootId() == 0) {
            // 根据获取到的上一级评论 的rootId  判断当前评论的rootId
            // rootId == 0 说明上一级评论是顶级评论 则代插入的评论的rootId 是一级评论的id
            replyBean.setRootId(observerBean.getId());
        } else {
            // rootId != 0 说明上一级评论是回复评论 则代插入的评论的rootId 是上一级评论的rootId
            replyBean.setRootId(observerBean.getRootId());
        }
        replyBean.setLastId(observerBean.getId());
        replyBean.setCreateTime(new Date());
        replyBean.setProductId(replyDto.getProductId());
        replyBean.setCustomerId(replyDto.getCustomerId());
        replyBean.setIsDelete(0);
        // 对谁回复
        replyBean.setReplyId(observerBean.getCustomerId());
        int insert = observerMapper.insert(replyBean);


        return insert == 1 ? 1 : 0;
    }

    /**
     * @description:删除根留言，并且删除所有回复
     * @author: wyj
     * @date: 2024/4/23 13:51
     * @param:
     * @return:
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteRootObserver(Integer observerId) {
        AssertUtil.isTrue(observerId == null, "参数异常");
        ObserverBean observerBean = observerMapper.selectById(observerId);
        AssertUtil.isTrue(observerBean == null, "留言不存在");
        // 查所有回复
        List<ObserverBean> list = observerMapper.getAllReplyByRootId(observerBean.getId());

        // 删除留言
        int i = observerMapper.deleteById(observerId);
        // 删除回复
        if (list.size() > 0) {
            for (ObserverBean bean : list) {
                int result = observerMapper.deleteById(bean.getId());
                AssertUtil.isTrue(result != 1, "删除失败");
            }
        }
        return i == 1 ? 1 : 0;
    }
}



